package PlugIn.commands;

import java.io.File;
import java.util.Collection;

import FilePanel.Data;
import PlugIn.PlugInModel;

/**
 * @author      Erwan Osouf, Lucas Ennouchi
 * @version     1.0                                
 * @since       29/01/2012
 */

public class Delete extends PlugInModel {
	
	
	public Delete(Data data) {
		super(data);
	}

	private String result;
	


	public String getResult() {
		return result;
	}

	/**
	 * sets the result
	 * @param result is set
	 */
	private void setResult(String result) {
		this.result = result;
	}

	/**
	 * manages when there is nothing to manage(with the command)
	 */
	protected void manageEmpty() {
		setResult("Rien � supprimer");

	}

	/**
	 * manages when we are dealing with files
	 */
	protected void manageFile(File f) {
		
		String s=f.delete() ? 
				"a �t� supprim�" : "n'a pas �t� supprim�";
		setResult("Le fichier " + s);
	}

	/**
	 * manages when we are dealing with Directories
	 */
	protected void manageDir(File f) {
		String s="";
		File[] listFiles=f.listFiles();
		if(listFiles.length>0) {
			for (File file : listFiles) {
				if (file.isDirectory())
					manageDir(file);
			else
				s=file.delete() ? 
						"a �t� supprim�" : "n'a pas �t� supprim�";
			}
		}
		s=f.delete() ?
			"a �t� supprim�" : "n'a pas �t� supprim�";
		setResult("Le dossier "+s);
	}

	/**
	 * manages when we are dealing with multiple file or directories
	 */
	protected void manageMultiple(Collection<File> lf) {
		boolean b=true;
		String s="";
		for (File file : lf) {
			if (file.isDirectory()) {
				manageDir(file);
				b&=getResult().equals("Le dossier a �t� supprim�");
			}
			else {
				manageFile(file);
				b&=getResult().equals("Le fichier a �t� supprim�");
			}
		}
		s=b?"a �t� supprim�e":"n'a pas �t� supprim�e";
		setResult("La collection "+s);
	}

	@Override
	public String getName() {
		return "Supprimer";
	}

}
